package net.hn.hnms.biz.blueprint.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.hn.hnms.biz.blueprint.domain.FileInfoRecord;
import net.hn.hnms.biz.blueprint.domain.FileTypeInfo;
import net.hn.hnms.biz.blueprint.mapper.FileInfoRecordMapper;
import net.hn.hnms.biz.blueprint.mapper.FileTypeInfoMapper;
import net.hn.hnms.biz.blueprint.service.IFileInfoRecordService;
import net.hn.hnms.biz.blueprint.vo.FileInfoPageVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ly
 * 发运统计表服务层实现
 * @date 2022-08-30
 */

@Service
public class FileInfoRecordServiceImpl extends BaseService<FileInfoRecord, FileInfoPageVO> implements IFileInfoRecordService {

    @Autowired
    private FileInfoRecordMapper fileInfoRecordMapper;

    @Autowired
    private FileTypeInfoMapper fileTypeInfoMapper;

    /*
    * 文件历史记录列表
    * */
    @Override
    public ResponsePage<FileInfoPageVO> selectPageList(String fileInfoId, PageQuery pageQuery,String mineCode,String companyCode) {
        LambdaQueryWrapper<FileInfoRecord> query = Wrappers.lambdaQuery();
        query.eq(FileInfoRecord::getFileInfoId,fileInfoId);
        query.eq(StrUtil.isNotBlank(mineCode),FileInfoRecord::getMineCode,mineCode);
        query.and(o->o.eq(StrUtil.isNotBlank(mineCode),FileInfoRecord::getMineCode,mineCode).or().eq(StrUtil.isNotBlank(companyCode),FileInfoRecord::getCompanyCode,companyCode));
        IPage<FileInfoRecord> pageResult = fileInfoRecordMapper.selectVoPage(pageQuery.build(), query);
        List<FileInfoPageVO> result = new ArrayList<>(pageResult.getRecords().size());
        pageResult.getRecords().forEach(item -> {
            LoginUser user = remoteUserService.getUserInfoById(Long.valueOf(item.getCreateUserId()));
            FileTypeInfo fileType = fileTypeInfoMapper.selectById(item.getFileTypeId());
            FileInfoPageVO vo = BeanUtil.copyProperties(item, FileInfoPageVO.class);
            vo.setUsername(user.getUsername());
            vo.setFileTypeName(fileType.getFileTypeName());
            result.add(vo);
        });
        return new ResponsePage(result, pageResult.getTotal());
    }
}
